package com.webtest.demo;


import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.PointerInput.Kind;
import org.openqa.selenium.interactions.PointerInput.MouseButton;
import org.openqa.selenium.interactions.PointerInput.Origin;
import org.openqa.selenium.interactions.Sequence;


import java.time.Duration;
import java.util.Arrays;


import io.appium.java_client.AppiumBy;

import io.appium.java_client.android.AndroidDriver;

/**
 * author:zhuying
 *
 */


public class BaseAction {
	public AndroidDriver driver;
	public Actions action;

	public BaseAction(AndroidDriver driver) {
		this.driver = driver;
		action = new Actions(driver);

	}

	public String getToastMsg(String string) {
		String msg=driver.findElement(AppiumBy.xpath("//*[@class='android.widget.Toast']")).getText();
		System.out.println(string+"："+msg);

		return msg;

	}
	
	
	//点击
	public void click(AppiumBy by) {
		driver.findElement(by).click();
	}

	public void click(WebElement elemnet) throws InterruptedException {
		elemnet.click();
		Thread.sleep(500);
	}
	
	//双击
	public void doubleClick(WebElement element) throws InterruptedException {
	    action.doubleClick(element).perform();
	    Thread.sleep(1000);
	}

	//长按
	public void longPress(WebElement ele) {
		action.clickAndHold(ele).perform();
	}
	
	//长按后释放
	public void longPress(WebElement ele, int durationInSeconds) {
	    action.clickAndHold(ele).perform();
	    action.pause(durationInSeconds * 100).release(ele).perform();
	}
	
	//获取页面某点的尺寸和坐标
	public void getPositionAndSize(WebDriver driver, By locator) throws InterruptedException {
	    WebElement element = driver.findElement(locator);
	    
	    Point location = element.getLocation();
	    Dimension size = element.getSize();
	    
	    System.out.println("Textbox coordinates - X: " + location.getX() + ", Y: " + location.getY());
	    System.out.println("Textbox size - Width: " + size.getWidth() + ", Height: " + size.getHeight());
	    Thread.sleep(1000);
	    
	}
	
	//点击坐标点
//    public void click(int x, int y) throws InterruptedException {
//        action.moveByOffset(x, y).click().perform();
//        Thread.sleep(1000);
//    }
	
    public void click(int x, int y) {
        PointerInput mouse = new PointerInput(Kind.MOUSE, "mouse");
        Sequence click = new Sequence(mouse, 0);
        click.addAction(mouse.createPointerMove(Duration.ZERO, Origin.viewport(), x, y));
        click.addAction(mouse.createPointerDown(MouseButton.LEFT.asArg()));
        click.addAction(mouse.createPointerUp(MouseButton.LEFT.asArg()));
        driver.perform(Arrays.asList(click));
    }
    
    
    //将鼠标在一个坐标点拖动到另一个坐标点
//    public void clickAndDrag(int startX, int startY, int endX, int endY) throws InterruptedException {
//        action.moveByOffset(startX, startY).clickAndHold().moveByOffset(endX - startX, endY - startY).release().perform();
//        Thread.sleep(1000);
//    }
    
    public void clickAndDrag(int startX, int startY, int endX, int endY) {
        PointerInput mouse = new PointerInput(Kind.MOUSE,"mouse1");
        Sequence drag = new Sequence(mouse, 0);
        drag.addAction(mouse.createPointerMove(Duration.ZERO, Origin.viewport(), startX, startY));
        drag.addAction(mouse.createPointerDown(MouseButton.LEFT.asArg()));
        drag.addAction(mouse.createPointerMove(Duration.ofMillis(1000), Origin.viewport(), endX, startY));
        drag.addAction(mouse.createPointerUp(MouseButton.LEFT.asArg()));
        driver.perform(Arrays.asList(drag));
    }
    
	//坐标点的滑动
  	public void swipe(int startX, int startY, int endX, int endY) {

		PointerInput finger = new PointerInput(Kind.TOUCH, "finger1");
		Sequence swipe = new Sequence(finger, 0);
		swipe.addAction(finger.createPointerMove(Duration.ZERO, Origin.viewport(), startX, startY));
		swipe.addAction(finger.createPointerDown(MouseButton.LEFT.asArg()));
		swipe.addAction(finger.createPointerMove(Duration.ofMillis(1000), Origin.viewport(), startX, endY));
		swipe.addAction(finger.createPointerUp(MouseButton.LEFT.asArg()));
		driver.perform(Arrays.asList(swipe));
	}


  	//拖动
	public void drag(WebElement from, WebElement to) {

		action.clickAndHold(from).moveToElement(to).release().perform();

	}

	public void setValue(AppiumBy by, String value) {
		driver.findElement(by).clear();
		driver.findElement(by).sendKeys(value);

	}


	//输入
	public void type(WebElement element, String context) {
		element.sendKeys(context);
	}

	public void typeAndClear(WebElement element, String context) {
		element.clear();
		element.sendKeys(context);
	}

	//输入键盘上的任意键
    public void pressKey(WebElement element, CharSequence key) {
        element.sendKeys(key);
    }

    //按下Enter键
    public void pressEnterKey() {
        action.sendKeys(Keys.ENTER).perform();
    }

    //按下Home键
    public void pressHomeKey() {
        action.sendKeys(Keys.HOME).perform();
    }
    
    //按下Tab键
    public void pressTabKey() {
    	action.sendKeys(Keys.TAB).perform();
    }

    //按下空格键
    public void pressSpaceKey() {
    	action.sendKeys(Keys.SPACE).perform();
    }
    
    //同时按下Shift和Tab键盘
    public void pressShiftTab() throws InterruptedException {  
    	action.sendKeys(Keys.chord(Keys.SHIFT, 
        		Keys.TAB, 
        		Keys.ENTER))
        .perform(); 
        Thread.sleep(300);
    }
    
    //同时按下键盘上的Ctrl + ？键，其中？指的是键盘上的任意一个键
    public void pressctrlkey(char key) {
        action.sendKeys(Keys.chord(Keys.CONTROL, 
        		String.valueOf(key)))
        .perform();
    }
    
    //同时按下键盘上的Ctrl + ？ + Enter键，其中？指的是键盘上的任意一个键
    public void pressctrlkeyenter(char key) {
        action.sendKeys(Keys.chord(Keys.CONTROL, 
        		String.valueOf(key), 
        		Keys.ENTER))
        .perform();
    }

    //按下特定的键码对应的键
    public void pressKeyCode(WebElement element, int keyCode) {
        element.sendKeys(Keys.valueOf(String.valueOf(keyCode)));
    }

    //按下特定的键盘键值对应的键
    public void pressKeyByName(WebElement element, String keyName) {
        element.sendKeys(Keys.valueOf(keyName));
    }
    
    //上传文件
    public void uploadFile(WebElement element, String filePath) {
        element.sendKeys(filePath);
    }
    
    //获取指定元素在页面上显示的文本内容
    public String getText(WebElement element) {
        return element.getText();
    }
    
    //页面放大
    public void zoomIn() {
        action.keyDown(Keys.CONTROL).sendKeys(Keys.ADD).keyUp(Keys.CONTROL).perform();
    }

    //页面缩小
    public void zoomOut() {
        action.keyDown(Keys.CONTROL).sendKeys(Keys.SUBTRACT).keyUp(Keys.CONTROL).perform();
    }
    
    //页面返回
    public void navigateBack() throws InterruptedException {
    	Thread.sleep(1000);
        driver.navigate().back();
    }
    
    //页面前进
    public void navigateForward() {
        driver.navigate().forward();
    }
    
    //获取弹窗
    public Alert switchToAlert() {
        return driver.switchTo().alert();
    }
    
    //获取弹窗文本内容
    public String getAlertText() {
        Alert alert = switchToAlert();
        return alert.getText();
    }
    
    //将鼠标移动到页面元素上
    public void moveToElement(WebElement element) {
        action.moveToElement(element).perform();
    }
    
    //将鼠标移动到页面元素上并点击
    public void moveToElementAndClick(WebElement element) {
        action.moveToElement(element).click().perform();
    }
    
    //用于实现鼠标点击并拖动
    public void dragAndDrop(WebElement sourceElement, WebElement targetElement) {
        action.dragAndDrop(sourceElement, targetElement).perform();
    }
    
    //鼠标点击并向左拖拽
    public void dragLeft(WebElement element) {
        action.clickAndHold(element).moveByOffset(-200, 0).release().perform();
    }

    //鼠标点击并向右拖拽
    public void dragRight(WebElement element) {
        action.clickAndHold(element).moveByOffset(50, 0).release().perform();
    }  

    //点击鼠标左键并向上拖动
//    public void dragUp(WebElement element) {
//        action.clickAndHold(element).moveByOffset(0, -400).release().perform();
//    }

    public void dragUp(WebElement element) {
        int dragDistance = 400; // 拖动的固定距离
        action.clickAndHold(element).moveToElement(element, 0, -dragDistance).release().perform();
    }
    
    //点击鼠标左键并向下拖动
    public void dragDown(WebElement element) {
        action.clickAndHold(element).moveByOffset(0, 50).release().perform();
    }
    
    //鼠标点击页面最右侧并向左拖拽
    public void dragLeftFromRightEdge() {
        int windowWidth = driver.manage().window().getSize().getWidth();
        action.clickAndHold().moveByOffset(-(windowWidth / 2), 0).release().perform();
    }

    //鼠标点击页面最右侧并向右拖拽
    public void dragRightFromRightEdge() {
        int windowWidth = driver.manage().window().getSize().getWidth();
        action.clickAndHold().moveByOffset(windowWidth / 2, 0).release().perform();
    }
    
    // 向上滚动鼠标滚轮
    public void scrollUp() {
        ((JavascriptExecutor)driver).executeScript("window.scrollBy(0, -250);");
        try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    // 向下滚动鼠标滚轮
    public void scrollDown(By locator) {
        WebElement element = driver.findElement(locator);
        action.moveToElement(element).sendKeys(Keys.PAGE_DOWN).perform();
        
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
//    //使用JavascriptExecutor来滚动页面直到注册按钮可见
//    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);");

    
    //刷新页面
    public void refreshPage() {
        driver.navigate().refresh();
    }
    
}
